<template>
    <div class="login-wrapper">
        <el-form :model="ruleForm"  :rules="rules" ref="loginForm"  class="login-ruleForm">
            <h3 class="login-title">叩丁狼客户管理系统</h3>
            <el-form-item prop="username">
                <el-input type="text" v-model="ruleForm.username" placeholder="请输入账号" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item  prop="pwd">
                <el-input type="password" v-model="ruleForm.pwd" placeholder="请输入密码" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button class="login-btn" type="primary" @click="loginFn">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import axios from 'axios'
import qs from 'qs'
export default{
    data(){
        return {
            ruleForm:{},
            rules:{
                username:[
                    {required:true, message:'请输入账号',trigger:'blur'},
                    {min:3, max:10, message:'账号长度是3-10',trigger:'blur'}
                ],
                pwd:[
                    {validator:this.validpwd,trigger:'blur'}
                ]
            }
        }
    },
    methods:{
       validpwd(rule,value,cb){
           console.log(rule,value,cb);
           //当前输入框的值是否为空
           if(!value) {
               cb('密码不能为空');
           }else{
               cb()
           }
       },
       loginFn(){
        console.log(this.$refs.loginForm);//获取里面的方法
        this.$refs.loginForm.validate((vali) =>{
            if(vali){
                console.log('通过校验')
                const params = {
                    username:this.ruleForm.username,
                    password:this.ruleForm.pwd,
                }
                //调用登录接口
                axios.post('/tokens',qs.stringify(params)).then(res =>{
                    if(res.data.success){
                        //进入后台管理
                    }else{
                        //提示错误
                        this.$message.error(res.data.msg)
                    }
                })
            }else{
                console.log('校验失败')
            }
        })
        }
    },
    
    
}
</script>

<style lang="less" scoped>
    .login-wrapper{
        display:flex;
        justify-content:center;
        align-items:center;
        background-color:#324057;
        height:100%;
        .login-ruleForm{
            width:350px;
            height:200px;
            .login-title{
                font-size:20px;
                color:#fff;
                text-align:center;
            }
            .login-btn{
                width:100%;
            }
        }
    }
</style>